package com.drug.lib.view.load;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.drug.lib.R;


public class LoadPage extends LinearLayout {

    private Builder builder;

    private boolean isFloating;
    private int image;
    private String message;
    private String progressText;

    public LoadPage(Context context) {
        this(context, null);
    }

    public LoadPage(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadPage(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LoadPage, defStyleAttr, 0);
        isFloating = a.getBoolean(R.styleable.LoadPage_loadpageIsFloating, false);
        image = a.getResourceId(R.styleable.LoadPage_loadpageBgImage, R.drawable.ui_content_tra_bg);
        message = a.getString(R.styleable.LoadPage_loadpageMessage);
        progressText = a.getString(R.styleable.LoadPage_loadpageProgressText);
        init(context);
    }

    private void init(Context mContext) {
        builder = new Builder(mContext, this)
                .setMessage(message)
                .setBgResource(image)
                .setProgress(progressText)
                .setMessage(message)
                .setShowView(isFloating);
    }


    public Builder getBuilder() {
        return builder;
    }

    public boolean isViEnable() {
        return getVisibility() == VISIBLE;
    }

    public interface RetryButtonClick {
        void onClickFromRetry();
    }


    public class Builder {
        private View view;
        private ViewGroup parentView;


        private LinearLayout errorLayout;
        public ImageView imageView;
        private TextView textView;
        private Button retryBtn;

        private LinearLayout progressLayout;
        private ProgressBar progressBar;
        private TextView textProgress;
        private Context mContext;
        private RetryButtonClick retryButtonClick;
        private LoadStatus loadStatus = LoadStatus.LOADING;

        private String progress;
        private int bgResource;
        private int errorResource;
        private String message;
        private String retryMessage;
        private boolean isFloating;

        public Builder(Context mContext, ViewGroup parentView) {
            this.mContext = mContext;
            this.parentView = parentView;
            initView();
            setEvent();
        }

        private void initView() {
            view = LayoutInflater.from(mContext).inflate(R.layout.view_empty_layout, parentView, false);

            errorLayout = view.findViewById(R.id.error_layout);
            imageView = view.findViewById(R.id.img_error_layout);
            textView = view.findViewById(R.id.tv_error_layout);
            retryBtn = view.findViewById(R.id.btn_Retry);


            progressLayout = view.findViewById(R.id.progress_layout);
            progressBar = view.findViewById(R.id.animProgress);
            textProgress = view.findViewById(R.id.textProgress);
            parentView.addView(view);
        }

        private void setEvent() {
            retryBtn.setOnClickListener(v -> {
                if (retryButtonClick != null) retryButtonClick.onClickFromRetry();
            });
        }

        public Builder setRetryButtonClick(RetryButtonClick retryButtonClick) {
            this.retryButtonClick = retryButtonClick;
            return this;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setProgress(String progress) {
            this.progress = progress;
            return this;
        }

        public Builder setBgResource(int bgResource) {
            this.bgResource = bgResource;
            return this;
        }

        public Builder setErrorResource(int errorResource) {
            this.errorResource = errorResource;
            return this;
        }

        public Builder setRetryMessage(String retryMessage) {
            this.retryMessage = retryMessage;
            return this;
        }

        public Builder setShowView(boolean isFloating) {
            this.isFloating = isFloating;
            parentView.setVisibility(isFloating ? VISIBLE : GONE);
            return this;
        }

        private void setTextProgress() {
            if (TextUtils.isEmpty(progress)) {
                textProgress.setText(R.string.loading);
            } else {
                textProgress.setText(progress);
            }
        }


        private void setErrorMessage() {
            if (TextUtils.isEmpty(message)) {
                textView.setText(R.string.error_view_no_data);
            } else {
                textView.setText(message);
            }
        }

        private void setRetryMessage() {
            if (TextUtils.isEmpty(retryMessage)) {
                retryBtn.setText(R.string.reload);
            } else {
                retryBtn.setText(retryMessage);
            }
        }

        private void setViewBackGround() {
            parentView.setBackgroundResource(bgResource);
        }

        private void setErrorImage() {
            if (errorResource != 0) imageView.setImageResource(errorResource);
        }

        public LoadStatus getLoadStatus() {
            return loadStatus;
        }

        public Builder dismiss() {
            show(LoadStatus.SUCCESS);
            return this;
        }


        public Builder show(LoadStatus loadStatus) {
            this.loadStatus = loadStatus;
            showParent();
            setLoadStatus();
            setViewInfo();
            return this;
        }

        private void showParent() {
            if (loadStatus.getStatus() == LoadStatus.SUCCESS.getStatus()) {
                if (isFloating) {
                    setShowView(false);
                    parentView.setAnimation(AlphaAnimationUtil.getAlphaAnimation());
                }
            } else {
                if (!isFloating) {
                    setShowView(true);
                }
            }
        }

        private void setViewInfo() {
            setMessage(loadStatus.getName());
            setErrorResource(loadStatus.getImage());
            setErrorImage();
            setViewBackGround();
            setRetryMessage();
            setErrorMessage();
            setTextProgress();
        }

        private Builder setLoadStatus() {

            switch (loadStatus) {
                case NETWORK_ERROR:
                    errorLayout.setVisibility(VISIBLE);
                    progressLayout.setVisibility(View.GONE);
                    setBgResource(R.drawable.ui_content_bg);
                    break;
                case LOADING:
                    errorLayout.setVisibility(GONE);
                    progressLayout.setVisibility(View.VISIBLE);
                    setBgResource(R.drawable.ui_content_tra_bg);
                    break;
                case NODATA:
                    errorLayout.setVisibility(VISIBLE);
                    progressLayout.setVisibility(View.GONE);
                    setBgResource(R.drawable.ui_content_bg);
                    break;
                case LOAD_FAILD:
                    errorLayout.setVisibility(VISIBLE);
                    progressLayout.setVisibility(View.GONE);
                    setBgResource(R.drawable.ui_content_bg);
                    break;
                case SUCCESS:
                    setBgResource(R.drawable.ui_content_tra_bg);
                    break;
            }
            return this;
        }
    }
}
